package com.example.travel.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.travel.common.AjaxResult;
import com.example.travel.common.AuthAccess;
import com.example.travel.common.Constants;
import com.example.travel.common.TableDataInfo;
import com.example.travel.domain.entity.Spot;
import com.example.travel.domain.request.PageQuery;
import com.example.travel.service.SpotService;
import io.swagger.annotations.Api;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author : LinQingLiang
 * @file : SpotController.java
 * @Since : 2023/2/5 12:37
 */

@RestController
@RequestMapping("/spot")
@Api(tags = "旅游景点")
public class SpotController extends BaseController{


    @Autowired
    private SpotService spotService;

    @Resource
    private RedisTemplate redisTemplate;

    /**
     * 分页查询
     * @param pageQuery
     * @param name
     * @return
     */
    @GetMapping("list")
    public TableDataInfo list(@Validated PageQuery pageQuery, @Param("name") String name){
        Page page1 = spotService.page(pageQuery.getPageNum(),pageQuery.getPageSize(),name);
        return getDataTable(page1.getRecords(),page1.getTotal());
    }


    /**
     *景点修改
     * @param spot
     * @return
     */
//    @CachePut(value = "spots",key = "'frontAll'") //更新缓存
    @PutMapping("/update")
    public AjaxResult updateSpot(@RequestBody Spot spot){
        spotService.updateById(spot);
        flushRedis(Constants.SPOTS_KEY);
        return AjaxResult.success();
    }


    /**
     * 删除某个景点
     * @param spotId
     * @return
     */
    //清除一条缓存，key为要清除的数据
//    @CacheEvict(value = "spot",key = "'frontAll'")
    @DeleteMapping("/{spotId}")
    public AjaxResult deleteSpot(@PathVariable Integer spotId){
        spotService.removeById(spotId);
        flushRedis(Constants.SPOTS_KEY);
        return AjaxResult.success();
    }


    /**
     * 批量删除的景点
     * @param spotIds
     * @return
     */
    @DeleteMapping("/deleteSpots/{spotIds}")
    public AjaxResult deleteSpots(@PathVariable List<Integer> spotIds){
        return AjaxResult.success(spotService.removeBatchByIds(spotIds));
    }


    /**
     * 添加旅游景点
     * @param spot
     * @return
     */
    @PostMapping("/addSpot")
    public AjaxResult addTravelSpot(@RequestBody Spot spot){
        QueryWrapper<Spot> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("spot_name",spot.getSpotName());
        Spot spot1 = spotService.getOne(queryWrapper);
        if (spot1 != null){
            return AjaxResult.error("当前景点已经存在");
        }
        flushRedis(Constants.SPOTS_KEY);
        spot.setType("jpg");
        spot.setValue(4);
        return AjaxResult.success(spotService.save(spot));
    }


    /**
     * 当前旅游景点信息
     * @param spotId
     * @return
     */
    @AuthAccess
    @GetMapping("/{spotId}")
    public AjaxResult selectById(@PathVariable Integer spotId){
        return AjaxResult.success(spotService.getById(spotId));
    }


    /**
     * 当前景点的用户
     * @param id
     * @return
     */
    @GetMapping("/userSpot/{id}")
    public AjaxResult userSelectSpot(@PathVariable Integer id){
        return AjaxResult.success(spotService.userSelectSpot(id));
    }


    @GetMapping("/spotName")
    public AjaxResult selectSpotNameList(){
        List<Spot> spotList = spotService.list();
        List nameList = spotList.stream().map(item ->item.getSpotName()).collect(Collectors.toList());
        return AjaxResult.success(nameList);
    }


    /**
     *
     * @param key
     */
    public void flushRedis(String key){
        redisTemplate.delete(key);
    }

}
